From: Joey Hess Date: Fri, 10 Jan 2025 17:27:51 +0000 (-0400) Subject: configs annex.post-update-command and annex.pre-commit-command X-Git-Tag: archive/raspbian/10.20250416-2+rpi1~1^2~10^2~15 X-Git-Url: https://dgit.raspbian.org/%22http:/www.example.com/cgi/%22https:/www.github.com/%22bookmarks:///%22http:/www.example.com/cgi/%22https:/www.github.com/%22bookmarks:/?a=commitdiff_plain;h=5df1b2b36e8ff2f233722e0f83c3aa892566702c;p=git-annex.git configs annex.post-update-command and annex.pre-commit-command Added git configs annex.post-update-command and annex.pre-commit-command that correspond to the git-annex hook scripts post-update-annex and pre-commit-annex. Note that the hook files take precience over the git config, since the git config can includ global config which should be overridden by local config. These new git configs are probably not super useful. Especially the pre-commit-annex hook is there to install scripts to instead of the pre-commit hook, since git-annex installs that hook itself. So why would someone want to use a git config for that? Only reason I can think of would be in a global git config. Or possibly because it's easier to set a git config than write a hook script, on an OS like Windows. The real reason I'm adding these is as groundwork for making other annex.*-command git configs also be available as hook scripts. I want to avoid having some things available as only git hooks and others as both gitconfigs and git hooks. (It seems that some annex.*-command configs don't translate to git hooks though.) In the man page, moved documentation of the hooks to be next to the documentation of the git configs. This is to avoid repitition. --- diff --git a/Annex/Branch.hs b/Annex/Branch.hs index 945acf724b..03adce0886 100644 --- a/Annex/Branch.hs +++ b/Annex/Branch.hs @@ -257,7 +257,7 @@ updateTo' pairs = do mergeIndex jl refs let commitrefs = nub $ fullname:refs ifM (handleTransitions jl localtransitions commitrefs) - ( runAnnexHook postUpdateAnnexHook + ( runAnnexHook postUpdateAnnexHook annexPostUpdateCommand , do ff <- if dirty then return False @@ -724,7 +724,7 @@ setIndexSha :: Git.Sha -> Annex () setIndexSha ref = do f <- fromRepo gitAnnexIndexStatus writeLogFile f $ fromRef ref ++ "\n" - runAnnexHook postUpdateAnnexHook + runAnnexHook postUpdateAnnexHook annexPostUpdateCommand {- Stages the journal into the index, and runs an action that - commits the index to the branch. Note that the action is run diff --git a/Annex/Hook.hs b/Annex/Hook.hs index 8c6d648fb0..deadf871ed 100644 --- a/Annex/Hook.hs +++ b/Annex/Hook.hs @@ -71,18 +71,26 @@ hookWarning h msg = do {- Runs a hook. To avoid checking if the hook exists every time, - the existing hooks are cached. -} -runAnnexHook :: Git.Hook -> Annex () -runAnnexHook hook = do +runAnnexHook :: Git.Hook -> (GitConfig -> Maybe String) -> Annex () +runAnnexHook hook commandcfg = do m <- Annex.getState Annex.existinghooks case M.lookup hook m of - Just True -> run - Just False -> noop + Just True -> runhook + Just False -> runcommandcfg Nothing -> do exists <- inRepo $ Git.hookExists hook Annex.changeState $ \s -> s { Annex.existinghooks = M.insert hook exists m } - when exists run + if exists + then runhook + else runcommandcfg where - run = unlessM (inRepo $ Git.runHook hook) $ do + runhook = unlessM (inRepo $ Git.runHook hook) $ do h <- fromRepo $ Git.hookFile hook - warning $ UnquotedString $ h ++ " failed" + commandfailed h + runcommandcfg = commandcfg <$> Annex.getGitConfig >>= \case + Just command -> + unlessM (liftIO $ boolSystem "sh" [Param "-c", Param command]) $ + commandfailed command + Nothing -> noop + commandfailed c = warning $ UnquotedString $ c ++ " failed" diff --git a/CHANGELOG b/CHANGELOG index b412a20602..42bcfa6fb1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -11,6 +11,9 @@ git-annex (10.20250103) UNRELEASED; urgency=medium * git-remote-annex: Use enableremote rather than initremote. * Windows: Fix permission denied error when dropping files that have the readonly attribute set. + * Added git configs annex.post-update-command and annex.pre-commit-command + that correspond to the git-annex hook scripts post-update-annex and + pre-commit-annex. -- Joey Hess Fri, 03 Jan 2025 14:30:38 -0400 diff --git a/Command/PreCommit.hs b/Command/PreCommit.hs index bc69e4a210..204a5fa8e2 100644 --- a/Command/PreCommit.hs +++ b/Command/PreCommit.hs @@ -44,7 +44,7 @@ seek ps = do -- files in the worktree won't be populated, so populate them here Command.Smudge.updateSmudged (Restage False) - runAnnexHook preCommitAnnexHook + runAnnexHook preCommitAnnexHook annexPreCommitCommand -- committing changes to a view updates metadata currentView >>= \case diff --git a/Types/GitConfig.hs b/Types/GitConfig.hs index 2ab5de3ea6..81904653f8 100644 --- a/Types/GitConfig.hs +++ b/Types/GitConfig.hs @@ -97,6 +97,8 @@ data GitConfig = GitConfig , annexAlwaysCompact :: Bool , annexCommitMessage :: Maybe String , annexCommitMessageCommand :: Maybe String + , annexPreCommitCommand :: Maybe String + , annexPostUpdateCommand :: Maybe String , annexMergeAnnexBranches :: Bool , annexDelayAdd :: Maybe Int , annexHttpHeaders :: [String] @@ -190,6 +192,8 @@ extractGitConfig configsource r = GitConfig , annexAlwaysCompact = getbool (annexConfig "alwayscompact") True , annexCommitMessage = getmaybe (annexConfig "commitmessage") , annexCommitMessageCommand = getmaybe (annexConfig "commitmessage-command") + , annexPreCommitCommand = getmaybe (annexConfig "pre-commit-command") + , annexPostUpdateCommand = getmaybe (annexConfig "post-update-command") , annexMergeAnnexBranches = getbool (annexConfig "merge-annex-branches") True , annexDelayAdd = getmayberead (annexConfig "delayadd") , annexHttpHeaders = getlist (annexConfig "http-headers") diff --git a/doc/git-annex.mdwn b/doc/git-annex.mdwn index 5e4c9f4777..1f499cb315 100644 --- a/doc/git-annex.mdwn +++ b/doc/git-annex.mdwn @@ -1155,6 +1155,24 @@ repository, using [[git-annex-config]]. See its man page for a list.) This command is run and its output is used as the commit message to the git-annex branch. +* `annex.post-update-command` + + This command is run after git-annex updates the git-annex branch. + + Alternatively, a hook script can be installed in + `.git/hooks/post-update-annex` + + When publishing a git-annex repository by http, this can be used to run + `git update-server-info` + +* `annex.pre-commit-command` + + This command is run whenever a commit is made to the HEAD branch of + the git repository, either by git commit, or by git-annex. + + Alternatively, a hook script can be installed in + `.git/hooks/pre-commit-annex` + * `annex.alwayscompact` By default, git-annex compacts data it records in the git-annex branch. @@ -2322,14 +2340,6 @@ used by git-annex. `~/.config/git-annex/autostart` is a list of git repositories to start the git-annex assistant in. -`.git/hooks/pre-commit-annex` in your git repository will be run whenever -a commit is made to the HEAD branch, either by git commit, git-annex -sync, or the git-annex assistant. - -`.git/hooks/post-update-annex` in your git repository will be run -whenever the git-annex branch is updated. You can make this hook run -`git update-server-info` when publishing a git-annex repository by http. - # SEE ALSO More git-annex documentation is available on its web site,